from openpyxl.styles import Alignment, Font
from openpyxl.utils import get_column_letter
from log.ulog import logger
import os

from utils.sheettools import uniformStyle
import configparser as config

from utils.tools import INSTRUCTIONS_TEXT

# 读取配置文件 settingObj.ini
config = config.ConfigParser()
config.read('config.ini')

filename = config.get('FILENAME', 'UNIFILENAME')
filepath = config.get('PATH', 'OUTPUT_PATH')
filesrc = os.path.join(filepath, filename)
print(filesrc)
uniformStyle(filesrc)

# 插入说明文档 INSTRUCTIONS_TEXT
import openpyxl

workbook = openpyxl.load_workbook(filesrc)
if '说明文档' in workbook.sheetnames:
    workbook.remove(workbook['说明文档'])
workbook.create_sheet('说明文档')
sheetbook = workbook['说明文档']
sheet = workbook.sheetnames
sheet_index = sheet.index('说明文档')
workbook.move_sheet(sheetbook, offset=-sheet_index)
border = openpyxl.styles.Border(left=openpyxl.styles.Side(border_style='thin', color='000000'),
                                right=openpyxl.styles.Side(border_style='thin', color='000000'),
                                top=openpyxl.styles.Side(border_style='thin', color='000000'),
                                bottom=openpyxl.styles.Side(border_style='thin', color='000000'))
# 插入说明文档
for index, text in enumerate(INSTRUCTIONS_TEXT, start=3):
    if index == 3:
        alignment = Alignment(horizontal='center',
                              vertical='center')
        font = Font(bold=True, size=24)
    else:
        alignment = Alignment(horizontal='left',
                              vertical='center',
                              wrap_text=True)
        font = Font(size=12)
        fillodd = openpyxl.styles.PatternFill(fill_type='solid',
                                              start_color='d9d9d9'
                                              )
    print(fr'index:{index}', text)
    sheetbook.row_dimensions[index].height = 45
    sheetbook.column_dimensions['C'].width = 150
    cell = sheetbook.cell(row=index, column=3, value=text)
    cell.alignment = alignment
    cell.font = font
    cell.border = border
    if index % 2 == 0:
        cell.fill = fillodd

try:
    workbook.remove(workbook['开发招标数据'])
    workbook.remove(workbook['开发中标数据'])
except Exception as e:
    logger.error('未找到删除的 sheet', e)
finally:
    workbook.save(filesrc)
